package tag.in.activities;

import tag.in.Constants;
import tag.in.R;
import tag.in.TagInApplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

/**
 * Set Options
 * 
 * @author andre
 */
public class TagOptionsActivity extends Activity {

    private static final String CLASSTAG = TagOptionsActivity.class.getSimpleName();
    private static final int MENU_OPTIONS = Menu.FIRST;
    private Button set_options_button;
    private EditText serverUrl;
    private EditText serverPort;
    private Spinner category;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(Constants.LOGTAG, " " + TagOptionsActivity.CLASSTAG + " onCreate");

        this.setContentView(R.layout.tag_options);

        this.serverUrl = (EditText) findViewById(R.id.server_url);
        this.serverPort = (EditText) findViewById(R.id.server_port);
        this.set_options_button = (Button) findViewById(R.id.set_options_button);
        this.category = (Spinner) findViewById(R.id.category);
        
        ArrayAdapter<String> categories = new ArrayAdapter<String>(this, R.layout.spinner_view, getResources()
            .getStringArray(R.array.categories));
        categories.setDropDownViewResource(R.layout.spinner_view_dropdown);
        this.category.setAdapter(categories);
        
        TagInApplication application = (TagInApplication) getApplication();
        this.serverUrl.setText(application.getServerUrl());
        this.serverPort.setText(application.getServerPort());
        
        this.set_options_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handleSetOptions();
            }
        });
    }   

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(Constants.LOGTAG, " " + TagOptionsActivity.CLASSTAG + " onResume");
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, TagOptionsActivity.MENU_OPTIONS, 0, R.string.set_options_button_label).setIcon(
            android.R.drawable.ic_menu_save);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case MENU_OPTIONS:
                handleSetOptions();
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
    
    private void handleSetOptions() {
        if (!validate()) {
            return;
        }
        
        TagInApplication application = (TagInApplication) getApplication();
        application.setServerUrl(this.serverUrl.getText().toString());
        application.setServerPort(this.serverPort.getText().toString());
        
        // call next Activity, VIEW_LOGIN
		Intent intent = new Intent(Constants.INTENT_ACTION_VIEW_LOGIN);
		startActivity(intent);
    }

    // validate form fields
    private boolean validate() {
        boolean valid = true;
        StringBuilder validationText = new StringBuilder();
        if ((this.serverUrl.getText() == null) || this.serverUrl.getText().toString().equals("")
        		|| (this.serverPort.getText() == null) || this.serverPort.getText().toString().equals("")
        		) {
            validationText.append(getResources().getString(R.string.fill_data));
            valid = false;
        }
        if (!valid) {
            new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.alert_label)).setMessage(
                validationText.toString()).setPositiveButton("Continuar",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        // in this case, don't need to do anything other than close alert
                    }
                }).show();
            validationText = null;
        }
        return valid;
    }
}
